﻿using System.Text;
using Microsoft.SharePoint;
using System.Web.UI;
using VPLink2013.Framework;
using VPLink2013.Framework.Constants;
using VPLink2013.Framework.Extensions;
using System;

namespace VPLink2013.Webcontrols
{
    public class Footer2013 : WebcontrolVPLink
    {
        protected override void Render(HtmlTextWriter writer)
        {
            Template = "webcontrol Footer2013";
            try
            {
                if (!string.IsNullOrEmpty(siteUrl))
                {
                    using (SPSite site = new SPSite(siteUrl))
                    {
                        using (SPWeb web = site.RootWeb)
                        {
                            SPList lst = web.Lists.TryGetList(ListTitles.VPFooterContentTitle);
                            if (lst != null)
                            {
                                ProcessLinksFooter2013(lst);
                            }
                        }
                    }
                }
            }
            catch (Exception exc)
            {
                ContextTrace.AddTrace("webcontrol OutilsPratiques.Erreur.Render : " + exc.Message + "/" + exc.StackTrace);
            }
            base.Render(writer);
        }

        private void ProcessLinksFooter2013(SPList lst)
        {
            SPQuery spQuery = new SPQuery();
            StringBuilder sb = new StringBuilder();
            sb.Append("<OrderBy><FieldRef Name='ID' Ascending='TRUE'/></OrderBy>");
            string Query = sb.ToString();
            spQuery.Query = Query;
            ContextTrace.AddTrace("spQuery.Query : " + spQuery.Query);

            SPListItemCollection items = lst.GetItems(spQuery);
            int intWidth = 100 / items.Count;

            sbHtml.AppendLine("<div id=\"links_footer\">");
            int iCrt = 0;
            foreach (SPListItem item in items)
            {
                try
                {
                    iCrt = CreateFooterItem(iCrt, item);
                }
                catch (Exception ex)
                {
                    ContextTrace.AddTrace("Exception lors de la creation d'un item FOOTER " + ex.Message);
                    ContextTrace.AddTrace(ex.StackTrace);
                }
            }

            string strPage = this.Context.Request.Url.AbsolutePath;
            strPage = strPage.ToLower();
            ContextTrace.AddTrace("strPage : " + strPage);
            
            if (strPage == string.Format("/pages/{0}", ListNames.SignatureVPName))
            {
                string strLink =  string.Format(@"{0}/services/DSI/RDB/Pages/{1}", siteUrl, ListNames.PolitiqueConfidentialitePName);
                string strURL = "<a href=\"" + strLink + "\" onclick=\"javascript:_paq.push(['trackPageView', 'RDB/Forum/Politique de confidentialite']);\" target=\"_new\">"
                    + "<span class='lienFooter2013'>Politique de confidentialite</span>"
                    + "</a>";
                
                sbHtml.AppendLine("&nbsp;|&nbsp;");
                sbHtml.AppendLine(strURL);
            }
            sbHtml.AppendLine("</div>");
        }

        private int CreateFooterItem(int iCrt, SPListItem item)
        {
            ContextTrace.AddTrace("-------------------------------");
            string strLinkURL = item.GetItemListFieldValueAsUrlValue(Fields.UrlNewsField); ;
            string strTexteURL = item.GetItemListFieldValueAsUrlDescription(Fields.UrlNewsField);
            string strTextePiwik = strTexteURL.Replace("/", "-").Replace("'", "\\'").Replace("\"", "\\'");
            ContextTrace.AddTrace("strTexteURL : " + strTexteURL);
            ContextTrace.AddTrace("strLinkURL : " + strLinkURL);

            string strCategorie = VPLink2013SPHelper.GetValue(item, "Catégorie");
            ContextTrace.AddTrace("strCategorie : " + strCategorie);

            string strURL = "<a href=\"" + strLinkURL + "\" onclick=\"javascript:_paq.push(['trackPageView', 'Footer/" + strTextePiwik + "']);\" target=\"_new\">"
                + strCategorie
                + "</a>";
            ContextTrace.AddTrace("strURL : " + strURL);

            if (iCrt > 0)
            {
                sbHtml.AppendLine("&nbsp;|&nbsp;");
            }
            sbHtml.AppendLine(strURL);

            iCrt += 1;
            return iCrt;
        }
    }
}
